package com.fwindcore.stage;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.fwindcore.res.ImageFactory;
import com.fwindcore.res.PublicConst;
import com.fwindcore.screen.MainScreen;
import com.fwindcore.util.ScreenPos;
import com.fwindcore.wheretorun.MyGdxGame;

public class LoadStage extends Stage {
	
	private Game game;
	private Image bgImg,titleImg;
	private ImageButton buttonStart;

	public LoadStage(Game game) {
		// TODO Auto-generated constructor stub
		this.game = game;
		this.create();
		this.init();
		this.addActor();
		
	}
	
	private void create(){
		bgImg = ImageFactory.getLoadBGImg();
		titleImg = ImageFactory.getTitleImg();
		buttonStart = new ImageButton(ImageFactory.getButtonStartImg().getDrawable());
		
	}
	
	private void init(){
		Gdx.input.setCatchBackKey(true);
		bgImg.setBounds(0, 0, PublicConst.WORLD_WIDTH, PublicConst.WORLD_HEIGHT);
		titleImg.setPosition(ScreenPos.getCenterX(titleImg.getWidth()), ScreenPos.getPercentY(0.80f));
		buttonStart.setPosition(ScreenPos.getCenterX(buttonStart.getWidth()),ScreenPos.getPercentY(0.20f));
		buttonStart.addListener(new InputListener(){
			@Override
			public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
		 		gameStart();
				return super.touchDown(event, x, y, pointer, button);
		 	}

		});
	}
	
	
	@Override
	public boolean keyDown(int keyCode) {
		// TODO Auto-generated method stub
		System.out.println("keyDown:" + keyCode);

		if (keyCode == 4 || keyCode == 131) {
			this.dispose();
			((MyGdxGame)game).exit();
//			this.dispose();
		}

		return super.keyDown(keyCode);
	}
	
	private void addActor(){
		this.addActor(bgImg);
		this.addActor(titleImg);
		this.addActor(buttonStart);
	}
	
	@Override
	public void act() {
		// TODO Auto-generated method stub
		Gdx.input.setInputProcessor(this);
		
		super.act();
	}

	private void gameStart(){
		game.setScreen(new MainScreen(game));
	}


}
